package com.cjh.wechat.core.controller;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cjh.wechat.commom.pojo.Group;
import com.cjh.wechat.commom.util.JSONUtil;
import com.cjh.wechat.commom.vo.GroupSearchCondition;
import com.cjh.wechat.commom.vo.PageVo;
import com.cjh.wechat.commom.vo.SearchGroupResultVo;
import com.cjh.wechat.commom.vo.ShowGroupMemberVo;
import com.cjh.wechat.core.service.GroupService;
import com.cjh.wechat.core.service.UserAndGroupService;

@Controller
@RequestMapping("/group")
public class GroupController
{
    @Resource
    private GroupService        groupService;
    @Resource
    private UserAndGroupService userAndGroupService;
    private static final int    PAGE_SIZE = 10;
    
    @ResponseBody
    @RequestMapping(value = "/agreeInvite", method = RequestMethod.POST)
    public String agreeInvite(Integer no, String gid)
    {
        groupService.agreeInvite(no, gid);
        return "success";
    }
    
    @ResponseBody
    @RequestMapping(value = "/refuseInvite", method = RequestMethod.POST)
    public String refuseInvite(Integer no, String gid)
    {
        groupService.refuseInvite(no, gid);
        return "success";
    }
    
    // 新增群成员信息
    @ResponseBody
    @RequestMapping(value = "/addGroup", method = RequestMethod.POST)
    public String addGroup(String uid, String name)
    {
        try
        {
            String gid = groupService.addGroup(uid, name);
            return gid;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return "fail";
        }
    }
    
    // 更新群成员信息
    @ResponseBody
    @RequestMapping(value = "/updateGroupMember", method = RequestMethod.POST)
    public String updateGroupMember(String uid, String gid, String type)
    {
        try
        {
            userAndGroupService.updateGroupMember(uid, gid, type);
            return "success";
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return "fail";
        }
    }
    
    // 踢人
    @ResponseBody
    @RequestMapping(value = "/delMember", method = RequestMethod.POST)
    public String delMember(String uid, String gid)
    {
        try
        {
            groupService.delMember(uid, gid);
            return "success";
        }
        catch (Exception e)
        {
            return "fail";
        }
    }
    
    // 退群
    @ResponseBody
    @RequestMapping(value = "/quitGroup", method = RequestMethod.POST)
    public String quitGroup(String uid, String gid)
    {
        try
        {
            groupService.quitGroup(uid, gid);
            return "success";
        }
        catch (Exception e)
        {
            return "fail";
        }
    }
    
    @ResponseBody
    @RequestMapping(value = "/findByGid", method = RequestMethod.GET)
    public String findByGid(String id)
    {
        String s = JSONUtil.objToJson(groupService.findByGid(id));
        return s;
    }
    
    @ResponseBody
    @RequestMapping(value = "/showGroup", method = RequestMethod.POST)
    public List<Group> showGroup(String id)
    {
        return groupService.findByUid(id);
    }
    
    @ResponseBody
    @RequestMapping(value = "/showGroupMember", method = RequestMethod.POST)
    public ShowGroupMemberVo showGroupMember(String id, String groupId)
    {
        ShowGroupMemberVo showGroupMemberVo = groupService.showGroupMember(id, groupId);
        return showGroupMemberVo;
    }
    
    @ResponseBody
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public PageVo search(@ModelAttribute GroupSearchCondition condition, String id, int page)
    {
        int total = groupService.getSearchNum(condition);
        List<SearchGroupResultVo> vos = groupService.search(condition, page, PAGE_SIZE, id);
        return buildPageVo(total, vos);
    }
    
    private PageVo buildPageVo(int total, List<SearchGroupResultVo> resultVos)
    {
        PageVo pageVo = new PageVo();
        if (total == 0)
        {
            pageVo.setPages(1);
        }
        else
        {
            pageVo.setPages(total % PAGE_SIZE == 0 ? total / PAGE_SIZE : total / PAGE_SIZE + 1);
        }
        pageVo.setData(resultVos);
        return pageVo;
    }
}
